const recogBehavior = Behavior({
  data:{
    infer_log: null,

    left_elbow: null,
    right_elbow: null,
    left_leg: null,
    right_leg: null,
    head: null,

    // pt_le: null,
    // pt_re: null,
    // pt_ll: null,
    // pt_rl: null,
    // pt_h: null,

    left_elbow_angle_log: 0.0,
    right_elbow_angle_log: 0.0,
    left_leg_angle_log: 0.0,
    right_leg_angle_log: 0.0,
    leaning_angle_log: 0.0,

    left_elbow_angle: 0.0,
    right_elbow_angle: 0.0,
    left_leg_angle: 0.0,
    right_leg_angle: 0.0,
    leaning_angle: 0.0,
    // 
    squat_down_leg_thresh: [60.0, 90.0],
    squat_up_leg_thresh: [150.0, 180.0],
    push_down_elbow_thresh: [70.0, 100.0],
    push_up_elbow_thresh: [150.0, 180.0],
    leaning_thresh: 20.0,
    stretching_thresh: 150.0,

    //
    origin: null,
    size: null, // w, h
  },
  
  methods: {
    getAngle(points) {
      // get 3 points
      // return angle
      // points: [x1, y1, x2, y2, x3, y3]
      // note as: a, b, c
      var ba = [points[0]-points[2], points[1]-points[3]]
      // var ac = [points[4]-points[0], points[5]-points[1]] 
      var bc = [points[4]-points[2], points[5]-points[3]] 
      
      var mod_ba = Math.sqrt(Math.pow(ba[0], 2)+Math.pow(ba[1], 2))
      var mod_bc = Math.sqrt(Math.pow(bc[0], 2)+Math.pow(bc[1], 2))

      var cos_b = (ba[0]*bc[0] + ba[1]*bc[1]) / (mod_ba*mod_bc)

      return Math.acos(cos_b) / Math.PI * 180
    },
    postProcess(anchor2DList) {
        //add this
        this.setData({infer_log: 'in [post-process]'})
        
        // this.setData({infer_log: anchor2DList.toString})
        // this.setData({infer_log: 'got anchor2DList in [post-process]'})

        //
        var pointsList = []
        // var bboxList = []
        anchor2DList.forEach(anchor => {
          anchor.points.forEach(point => {
            const {
              x,
              y
            } = point
            pointsList.push(x * 2 - 1, 1 - y * 2)
          })
        })

        this.setData({origin: [anchor2DList[0].origin.x, anchor2DList[0].origin.y],
                      size: [anchor2DList[0].size.width, anchor2DList[0].size.height]})


        this.setData({infer_log: 'after loop in [post-process]'})

        var pt_le = [pointsList[10].toFixed(2), pointsList[11].toFixed(2), 
                      pointsList[14].toFixed(2), pointsList[15].toFixed(2), 
                      pointsList[18].toFixed(2), pointsList[19].toFixed(2)]
        var pt_re = [pointsList[12].toFixed(2), pointsList[13].toFixed(2), 
                      pointsList[16].toFixed(2), pointsList[17].toFixed(2), 
                      pointsList[20].toFixed(2), pointsList[21].toFixed(2)]
        var pt_ll = [pointsList[22].toFixed(2), pointsList[23].toFixed(2), 
                      pointsList[26].toFixed(2), pointsList[27].toFixed(2), 
                      pointsList[30].toFixed(2), pointsList[31].toFixed(2)]
        var pt_rl = [pointsList[24].toFixed(2), pointsList[25].toFixed(2), 
                      pointsList[28].toFixed(2), pointsList[29].toFixed(2), 
                      pointsList[32].toFixed(2), pointsList[33].toFixed(2)]
        var pt_h = [pointsList[0].toFixed(2), pointsList[1].toFixed(2)]
        // 上半身前倾程度 
        // 5, 11, 13
        var pt_up = [pointsList[10].toFixed(2), pointsList[11].toFixed(2), 
                      pointsList[22].toFixed(2), pointsList[23].toFixed(2), 
                      pointsList[26].toFixed(2), pointsList[27].toFixed(2)]

        this.setData({left_elbow_angle: this.getAngle(pt_le),
                      right_elbow_angle: this.getAngle(pt_re),
                      left_leg_angle: this.getAngle(pt_ll),
                      right_leg_angle: this.getAngle(pt_rl),
                      leaning_angle: this.getAngle(pt_up)})

        this.setData({left_elbow_angle_log: this.data.left_elbow_angle.toString(),
                      right_elbow_angle_log: this.data.right_elbow_angle.toString(),
                      left_leg_angle_log: this.data.left_leg_angle.toString(),
                      right_leg_angle_log: this.data.right_leg_angle.toString(),
                      leaning_angle_log: this.data.leaning_angle_log.toString()})


        this.setData({left_elbow: pt_le.toString()})
        this.setData({right_elbow: pt_re.toString()})
        this.setData({left_leg: pt_ll.toString()})
        this.setData({right_leg: pt_rl.toString()})
        this.setData({head: pt_h.toString()})
        this.setData({infer_log: 'after update'})
    },
    squatDownJudge(){
      if(this.data.left_leg_angle < this.data.squat_down_leg_thresh[1] &&
        this.data.left_leg_angle > this.data.squat_down_leg_thresh[0] &&
        this.data.right_leg_angle < this.data.squat_down_leg_thresh[1] &&
        this.data.right_leg_angle > this.data.squat_down_leg_thresh[0]) {
          return true
        }
      return false
    },
    squatUpJudge(){
      if(this.data.left_leg_angle < this.data.squat_up_leg_thresh[1] &&
        this.data.left_leg_angle > this.data.squat_up_leg_thresh[0] &&
        this.data.right_leg_angle < this.data.squat_up_leg_thresh[1] &&
        this.data.right_leg_angle > this.data.squat_up_leg_thresh[0]) {
          return true
        }
      return false
    },
    pushDownJudge(){
      if(this.data.left_elbow_angle < this.data.push_down_elbow_thresh[1] &&
        this.data.left_elbow_angle > this.data.push_down_elbow_thresh[0] &&
        this.data.right_elbow_angle < this.data.push_down_elbow_thresh[1] &&
        this.data.right_elbow_angle > this.data.push_down_elbow_thresh[0]) {
          return true
        }
      return false
    },
    pushUpJudge(){
      if(this.data.left_elbow_angle < this.data.push_up_elbow_thresh[1] &&
        this.data.left_elbow_angle > this.data.push_up_elbow_thresh[0] &&
        this.data.right_elbow_angle < this.data.push_up_elbow_thresh[1] &&
        this.data.right_elbow_angle > this.data.push_up_elbow_thresh[0]) {
          return true
        }
      return false
    }
  },
})

export default recogBehavior